"""
from threading import Lock

lock = Lock() # 实例化锁对象
lock.acquire() # 上锁,如果lock已经上锁,再调用会阻塞线程
lock.release() # 解锁

with lock: 上锁
...
....
        with 代码块执行结束,自动解锁

"""

from threading import Thread, Lock

lock = Lock() # 实例化锁
a = b = 0

def value():
    while True:
        lock.acquire() # 上锁
        if a!=b:
            print(f"a={a}, b={b}")
        lock.release()  # 解锁


s = Thread(target=value)
s.start()

while True:
    with lock: # 所有线程执行代码的地方必须都要上锁
        a+=1
        b+=1
s.join()
